{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pymysql\n",
    "import urllib\n",
    "import urllib.request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fetchall():\n",
    "    conn = pymysql.Connection(host='localhost', user='root', password='dtz384', port=3306, db='代理ip', charset='utf8')\n",
    "    cur = conn.cursor()\n",
    "    data = cur.execute('select * from 代理IP')\n",
    "    conn.commit()\n",
    "    return cur.fetchall()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "socket_list = []\n",
    "for row in fetchall():\n",
    "    ip,a,b,c,port,d,e = row\n",
    "    one = str(ip)+':'+ str(port)\n",
    "    socket_list.append(one)\n",
    "print(socket_list[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "url = 'https://www.baidu.com/'\n",
    "param = ''\n",
    "import urllib.parse;\n",
    "import socket;\n",
    "\n",
    " # 获取用户浏览器信息\n",
    "userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0'\n",
    "\n",
    "# 进行数据提交\n",
    "\n",
    "def run(url):\n",
    "    proxyIpList = socket_list\n",
    "#data = urllib.parse.urlencode(param).encode(encoding='utf8')\n",
    "    req = urllib.request.Request(url)\n",
    "    req.add_header('User-Agent', userAgent)\n",
    "    for proxyIp in proxyIpList:\n",
    "        socket.setdefaulttimeout(3);  # 3秒未响应则为超时，跳过执行下一条\n",
    "        try:\n",
    "# 添加代理\n",
    "            proxy_handler = urllib.request.ProxyHandler({'http': proxyIp});\n",
    "            proxy_auth_handler = urllib.request.ProxyBasicAuthHandler();\n",
    "            opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler);\n",
    "# 添加头信息\n",
    "            opener.addheaders = [('User-Agent', userAgent)]\n",
    "\n",
    "# 数据请求\n",
    "            response = opener.open(url, data);\n",
    "# 获取请求返还数据\n",
    "            response_data = response.read().decode(\"utf8\");\n",
    "            print(proxyIp, \"正确\");\n",
    "# return response_data;\n",
    "        except urllib.error.HTTPError as e:\n",
    "            print(proxyIp, \"错误：错误代码：\", e.code);\n",
    "# print(\"错误内容：\", e.read().decode(\"utf8\"));\n",
    "        except urllib.error.URLError as e:\n",
    "            print(proxyIp, '错误：未能获取服务器信息.');\n",
    "# print('错误原因: ', e.reason);\n",
    "        except:\n",
    "            print(proxyIp, \"错误：其他未知错误！\");\n",
    "run(url, param)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
